package com.stackoverflow;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class ProfileDisplay extends Activity implements OnClickListener {
	
	private static final int SEARCH_DIALOG_ID = 0;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.profile);
	    showDialog(SEARCH_DIALOG_ID);
	    
	    String userName = getIntent().getStringExtra("tagName");
	    
	    if (userName != null)
	    {
	    	Retriever.getInstance().getProfile().parseUserString(userName);
	    	Retriever.getInstance().getProfile().setSavedProfileData(userName, this);
	    	
		    setDisplay();
	    }
	    
	    View changeUserButton = findViewById(R.id.changeUser);
	    changeUserButton.setOnClickListener(this);
	    
	    View viewQuestionsButton = findViewById(R.id.viewQuestions);
	    viewQuestionsButton.setOnClickListener(this);
	}
	
	public void onClick(View v) {
		switch (v.getId()) {
    		case R.id.changeUser:
    			showDialog(SEARCH_DIALOG_ID);
    			break;
    		case R.id.viewQuestions:
    			Intent i = new Intent(ProfileDisplay.this, QuestionList.class);
    			i.putExtra("tagName", "{user}"+Retriever.getInstance().getProfile().getName()+", "
    								  +Retriever.getInstance().getProfile().getReputation()
    								  +";ID="+Retriever.getInstance().getProfile().getId());
            	startActivity(i);
    			break;
		}
	}
	
	public void onResume()
	{
		super.onResume();

	    if (Retriever.getInstance().getProfile().getSavedProfileData(this))
	    {
		    setDisplay();
	    	dismissDialog(SEARCH_DIALOG_ID);
	    }
	}
	
	public void setDisplay()
	{
		TextView userNameDisplay = (TextView) findViewById(R.id.userName);
	    userNameDisplay.setText(Retriever.getInstance().getProfile().getName());
	    
	    TextView reputationDisplay = (TextView) findViewById(R.id.reputation);
	    reputationDisplay.setText(Retriever.getInstance().getProfile().getReputation() + " reputation points");
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {

	    if (Retriever.getInstance().getProfile().getSavedProfileData(this))
	    {
	    	setDisplay();
	    }
	    
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	final View layout = inflater.inflate(R.layout.search_dialog, (ViewGroup) findViewById(R.id.root));  	    	

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Search for User").setView(layout);
        builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
                	ProfileDisplay.this.dismissDialog(SEARCH_DIALOG_ID);
                }
            });
		
			builder.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int whichButton) {
                        	ProfileDisplay.this.dismissDialog(SEARCH_DIALOG_ID);
                        	EditText search_field = (EditText) layout.findViewById(R.id.search_field);
                        	
                        	Intent i = new Intent(ProfileDisplay.this, UserList.class);
                        	
                        	String userName = search_field.getText().toString();

                        	i.putExtra("userName", userName);
                        	i.putExtra("profilePage", true);
                        	startActivity(i);
						}                	
                    });
			return builder.create();
    }
    
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        
        AlertDialog a = (AlertDialog) dialog;
    	EditText name_field = (EditText) a.findViewById(R.id.search_field);
    	name_field.setText("");
    }

}
